﻿/** 
 * Copyright 2010 NUMKIT.COM (contact: opensource@numkit.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @author 陈超
 * @version 1.0
 */
import flash.external.ExternalInterface;
class DrawPanel
{
	static public var SCALE_MIN=0.2;
	static public var SCALE_MAX=2;
	private var width:Number=0;
	private var height:Number=0;
	private var aPlace:Array = new Array ();
	private var aRoad:Array=new Array();
	private var aPolygon:Array=new Array();
	private var rootMC:MovieClip;
	private var bkMC:MovieClip;
	private var syncMC:MovieClip;
	private var asynMC:MovieClip;
	private var tipTF:TextField;
	private var scale:Number=1;
	private var drawingDone:Boolean=false;
	public function DrawPanel(rootMC:MovieClip){
		this.rootMC=rootMC;
		//缓存位图表面优化回放
		//rootMC.cacheAsBitmap=true;
	}
	
	public function getWidth():Number{
		return width;
	}
	
	public function getHeight():Number{
		return height;
	}
	
	public function loadExtPlace(root:XMLNode):Void{
		if (root.nodeName != "ROOT"){
			throw new Error ("Can't find ROOT Node in map data!");
		}
		for (var child:XMLNode = root.firstChild; child != null; child = child.nextSibling){
			var nodeName:String = child.nodeName;
			if(nodeName=="PL"){
				var place:Place=new Place(child);
				place.setHighlighted(true);
				aPlace.push(place);
			}
		}
	}
	
	public function loadMap(root:XMLNode):Void{
		if (root.nodeName != "ROOT"){
			throw new Error ("Can't find ROOT Node in external places data!");
		}
		var configNode:XMLNode = root.firstChild;
		if (configNode.nodeName != "Config"){
			throw new Error ("Not a Config Node!");
		}
		var widthNode:XMLNode = configNode.firstChild;
		if (widthNode.nodeName != "Width"){
			throw new Error ("Not a Width Node!");
		}
		this.width = parseInt (widthNode.firstChild.nodeValue);
		var heightNode:XMLNode = configNode.lastChild;
		if (heightNode.nodeName != "Height"){
			throw new Error ("Not a Height Node!");
		}
		this.height = parseInt (heightNode.firstChild.nodeValue);
		var mapNode:XMLNode = root.lastChild;
		if (mapNode.nodeName != "Map"){
			throw new Error ("Not A Map Node!");
		}
		for (var child:XMLNode = mapNode.firstChild; child != null; child = child.nextSibling){
			var nodeName:String = child.nodeName;
			if (nodeName == "PG"){
				aPolygon.push(new Polygon(child));
			}else if(nodeName=="PL"){
				aPlace.push(new Place(child));
			}else if(nodeName=="RD"){
				aRoad.push(new Road(child));
			}
		}
	}
	
	public function drawMap():Void{
		//绘制地图背景
		bkMC = rootMC.createEmptyMovieClip("bkMC",rootMC.getNextHighestDepth());
		//EBE6DC
		//FEEFFE
		//EBEAE8 ?
		bkMC.beginFill(0xFEEFFE, 100);
		bkMC.moveTo(0, 0);
		bkMC.lineTo(width, 0);
		bkMC.lineTo(width, height);
		bkMC.lineTo(0, height);
		bkMC.lineTo(0, 0);
		bkMC.endFill();
		//同步及异步缩放MovieClip
		syncMC = rootMC.createEmptyMovieClip("syncMC", rootMC.getNextHighestDepth());
		asynMC = rootMC.createEmptyMovieClip("asynMC", rootMC.getNextHighestDepth());
		//跟随鼠标的说明文本
		var formatter:TextFormat = new TextFormat();
		formatter.size = 12;
		tipTF = rootMC.createTextField("tipTF", rootMC.getNextHighestDepth(),
			-100,-100,50,18);
		tipTF.setTextFormat(formatter);
		tipTF.selectable=false;
		tipTF._visible=false;
		Polygon.setTipTF(tipTF);
		Road.setTipTF(tipTF);
		Place.setTipTF(tipTF);
		
		var tempMC:MovieClip;
		for(var i:Number=0;i<aPolygon.length;i++){
			tempMC=syncMC.createEmptyMovieClip("Polygon"+i, 
				syncMC.getNextHighestDepth());
			//tempMC.cacheAsBitmap=true;
			aPolygon[i].drawGraphicPart(tempMC);
		}
		for(var i:Number=0;i<aRoad.length;i++){
			tempMC=syncMC.createEmptyMovieClip("RoadGraphic"+i, 
				syncMC.getNextHighestDepth());
			//tempMC.cacheAsBitmap=true;
			aRoad[i].drawGraphicPart(tempMC);
			tempMC=asynMC.createEmptyMovieClip("RoadText"+i, 
				asynMC.getNextHighestDepth());
			//tempMC.cacheAsBitmap=true;
			aRoad[i].drawTextPart(tempMC);
		}
		for(var i:Number=0;i<aPlace.length;i++){
			tempMC=asynMC.createEmptyMovieClip("PlaceGraphic"+i, 
				asynMC.getNextHighestDepth());
			//tempMC.cacheAsBitmap=true;
			aPlace[i].drawGraphicPart(tempMC);
			tempMC=asynMC.createEmptyMovieClip("PlaceText"+i, 
				asynMC.getNextHighestDepth());
			//tempMC.cacheAsBitmap=true;
			aPlace[i].drawTextPart(tempMC);
		}
		//计算拖动范围
		rootMC.dragLeftBound=Stage.width-width*scale;
		rootMC.dragTopBound=Stage.height-height*scale;
        //缩放及拖动功能
		Mouse.addListener(this);
	}
	
	public function setDrawingDone(done:Boolean):Void{
		this.drawingDone=done;
	}
	
	public function isDrawingDone():Boolean{
		return drawingDone;
	}
	
	public function onMouseWheel(delta:Number):Void{
	}
	
	public function onMouseDown():Void{
	}
	
	public function onMouseUp():Void{
	}
	
	public function onMouseMove():Void{
		if(tipTF!=undefined){
			tipTF._x=rootMC._xmouse;
			tipTF._y=rootMC._ymouse-14;
			tipTF.updateAfterEvent();
		}
	}
	
	public function moveTo(x:Number,y:Number):Void{
		if(isNaN(x)||isNaN(y))
			return;
		//改变图层位置
		rootMC._x=Stage.width/2-x*scale;
		rootMC._y=Stage.height/2-y*scale;
		//若地图位置超出拖动范围，调整
		if(rootMC._x<rootMC.dragLeftBound)
			rootMC._x=rootMC.dragLeftBound;
		if(rootMC._x>0)
			rootMC._x=0;
		if(rootMC._y<rootMC.dragTopBound)
			rootMC._y=rootMC.dragTopBound;
		if(rootMC._y>0)
			rootMC._y=0;
	}
	
	public function getCenterPos():Point{
		var centerX:Number=(Stage.width/2-rootMC._x)/this.scale;
		var centerY:Number=(Stage.height/2-rootMC._y)/this.scale;
		return new Point(centerX,centerY);
	}
	
	public function getViewPortRect():Rect{
		return new Rect(-rootMC._x/this.scale,(Stage.width-rootMC._x)/this.scale,
			-rootMC._y/this.scale,(Stage.height-rootMC._y)/this.scale);
	}
	
	public function setScale(scale:Number):Void{
		if(isNaN(scale))
			return;
		if(scale<SCALE_MIN){
			scale=SCALE_MIN;
		}
		if(scale>SCALE_MAX){
			scale=SCALE_MAX;
		}
		if(this.scale!=scale){
			var centerX:Number=(Stage.width/2-rootMC._x)/this.scale;
			var centerY:Number=(Stage.height/2-rootMC._y)/this.scale;
			bkMC._xscale=bkMC._xscale*scale/this.scale;
			bkMC._yscale=bkMC._yscale*scale/this.scale;
			syncMC._xscale=syncMC._xscale*scale/this.scale;
			syncMC._yscale=syncMC._yscale*scale/this.scale;
			for(var i:Number=0;i<aRoad.length;i++){
				aRoad[i].setScale(scale);
			}
			for(var i:Number=0;i<aPlace.length;i++){
				aPlace[i].setScale(scale);
			}
			//修正中心位置
			rootMC._x=Stage.width/2-centerX*scale;
			rootMC._y=Stage.height/2-centerY*scale;
			//更新拖动范围
			rootMC.dragLeftBound=Stage.width-width*scale;
			rootMC.dragTopBound=Stage.height-height*scale;
			//若地图位置超出拖动范围，调整
			if(rootMC._x<rootMC.dragLeftBound)
				rootMC._x=rootMC.dragLeftBound;
			if(rootMC._x>0)
				rootMC._x=0;
			if(rootMC._y<rootMC.dragTopBound)
				rootMC._y=rootMC.dragTopBound;
			if(rootMC._y>0)
				rootMC._y=0;
			this.scale=scale;
		}
	}
	
	public function getScale():Number{
		return scale;
	}
	
	public function setDefaultRoadLink(link:String):Void{
		if(link!=undefined&&link!="")
			Road.setDefaultLink(link);
	}
	public function setDefaultPlaceLink(link:String):Void{
		if(link!=undefined&&link!="")
			Place.setDefaultLink(link);
	}
	public function setDefaultPolygonLink(link:String):Void{
		if(link!=undefined&&link!="")
			Polygon.setDefaultLink(link);
	}
	
	public function setCenterPlace(str:String,id:Number):Boolean{
		if(str!=undefined&&str!=null){
			if(id==undefined||id==null){
				for(var i:Number=0;i<aPlace.length;i++){
					if(aPlace[i].getName()==str){
						moveTo(aPlace[i].getX(),aPlace[i].getY());
						return true;
					}
				}
			}else{
				for(var i:Number=0;i<aPlace.length;i++){
					if(aPlace[i].getName()==str&&aPlace[i].getID()==id){
						moveTo(aPlace[i].getX(),aPlace[i].getY());
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public function setCenterRoad(str:String,id:Number):Boolean{
		if(str!=undefined&&str!=null){
			if(id==undefined||id==null){
				for(var i:Number=0;i<aRoad.length;i++){
					if(aRoad[i].getName()==str){
						moveTo(aRoad[i].getCenterX(),aRoad[i].getCenterY());
						return true;
					}
				}
			}else{
				for(var i:Number=0;i<aRoad.length;i++){
					if(aRoad[i].getName()==str&&aRoad[i].getID()==id){
						moveTo(aRoad[i].getCenterX(),aRoad[i].getCenterY());
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public function setCenterPolygon(str:String,id:Number):Boolean{
		if(str!=undefined&&str!=null){
			if(id==undefined||id==null){
				for(var i:Number=0;i<aPolygon.length;i++){
					if(aPolygon[i].getName()==str){
						moveTo(aPolygon[i].getCenterX(),aPolygon[i].getCenterY());
						return true;
					}
				}
			}else{
				for(var i:Number=0;i<aPolygon.length;i++){
					if(aPolygon[i].getName()==str&&aPolygon[i].getID()==id){
						moveTo(aPolygon[i].getCenterX(),aPolygon[i].getCenterY());
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public function getExternalPlacesName():String{
		var result:String="";
		if(aPlace!=undefined&&aPlace.length!=0){
			var first:Boolean=true;
			for(var i:Number=0;i<aPlace.length;i++){
				if(aPlace[i].isHighlighted()){
					if(first){
						result+=aPlace[i].getName();
						first=false;
					}else{
						result+=","+aPlace[i].getName();
					}
				}
			}
		}
		return result;
	}
	
	public function getAllPlacesName():String{
		var result:String="";
		if(aPlace!=undefined&&aPlace.length!=0){
			var first:Boolean=true;
			for(var i:Number=0;i<aPlace.length;i++){
				if(first){
					result+=aPlace[i].getName();
					first=false;
				}else{
					result+=","+aPlace[i].getName();
				}
			}
		}
		return result;
	}
	
	public function getOriginPlacesName():String{
		var result:String="";
		if(aPlace!=undefined&&aPlace.length!=0){
			var first:Boolean=true;
			for(var i:Number=0;i<aPlace.length;i++){
				if(!aPlace[i].isHighlighted()){
					if(first){
						result+=aPlace[i].getName();
						first=false;
					}else{
						result+=","+aPlace[i].getName();
					}
				}
			}
		}
		return result;
	}
	
	public function resetExternalPlaces(root:XMLNode):Boolean{
		if (root.nodeName != "ROOT"){
			return false;
		}
		//删除旧附加地点
		for(var i:Number=0;i<aPlace.length;i++){
			if(aPlace[i].isHighlighted()){
				aPlace[i].getGraphicMC().removeMovieClip();
				aPlace[i].getTextMC().removeMovieClip();
				aPlace.splice(i,1);
				i--;
			}
		}
		//添加新附加地点
		for (var child:XMLNode = root.firstChild; child != null; child = child.nextSibling){
			var nodeName:String = child.nodeName;
			if(nodeName=="PL"){
				var place:Place=new Place(child);
				place.setHighlighted(true);
				aPlace.push(place);
			}
		}
		//绘制新附加地点
		var tempMC:MovieClip;
		for(var i:Number=0;i<aPlace.length;i++){
			if(aPlace[i].isHighlighted()){
				tempMC=asynMC.createEmptyMovieClip("PlaceGraphic"+i, 
					asynMC.getNextHighestDepth());
				//tempMC.cacheAsBitmap=true;
				aPlace[i].drawGraphicPart(tempMC);
				tempMC=asynMC.createEmptyMovieClip("PlaceText"+i, 
					asynMC.getNextHighestDepth());
				//tempMC.cacheAsBitmap=true;
				aPlace[i].drawTextPart(tempMC);
				aPlace[i].setScale(scale);
			}
		}
		rootMC.updateAfterEvent();
		return true;
	}
	
	public function setOutstandPlace(str:String, id:Number, outstand:Boolean):Boolean{
		if(str!=undefined&&str!=null){
			if(id==undefined||id==null){
				for(var i:Number=0;i<aPlace.length;i++){
					if(aPlace[i].getName()==str){
						aPlace[i].setOutStand(outstand);
						return true;
					}
				}
			}else{
				for(var i:Number=0;i<aPlace.length;i++){
					if(aPlace[i].getName()==str&&aPlace[i].getID()==id){
						aPlace[i].setOutStand(outstand);
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public function setOutstandRoad(str:String, id:Number, outstand:Boolean):Boolean{
		if(str!=undefined&&str!=null){
			if(id==undefined||id==null){
				for(var i:Number=0;i<aRoad.length;i++){
					if(aRoad[i].getName()==str){
						aRoad[i].setOutStand(outstand);
						return true;
					}
				}
			}else{
				for(var i:Number=0;i<aRoad.length;i++){
					if(aRoad[i].getName()==str&&aRoad[i].getID()==id){
						aRoad[i].setOutStand(outstand);
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public function setOutstandPolygon(str:String, id:Number, outstand:Boolean):Boolean{
		if(str!=undefined&&str!=null){
			if(id==undefined||id==null){
				for(var i:Number=0;i<aPolygon.length;i++){
					if(aPolygon[i].getName()==str){
						aPolygon[i].setOutStand(outstand);
						return true;
					}
				}
			}else{
				for(var i:Number=0;i<aPolygon.length;i++){
					if(aPolygon[i].getName()==str&&aPolygon[i].getID()==id){
						aPolygon[i].setOutStand(outstand);
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public function clearAllOutstand():Void{
		for(var i:Number=0;i<aPlace.length;i++){
			aPlace[i].setOutStand(false);
		}
		for(var i:Number=0;i<aRoad.length;i++){
			aRoad[i].setOutStand(false);
		}
		for(var i:Number=0;i<aPolygon.length;i++){
			aPolygon[i].setOutStand(false);
		}
	}
	
}
